<template>
  <div>
    <div align="center" style="margin-bottom: 20px; font-size: 30px"><b>{{this.record.stockName}}</b></div>
    <!--  #####################查询条件###################  -->
<!--    <div align="center" style="margin-bottom: 20px; font-size: 30px"><b>{{this.record.stockName}}</b></div>-->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="使用状态">
              <a-select v-model="queryParam.status" placeholder='请选择' allowClear>
                <a-select-option value="1">未使用</a-select-option>
                <a-select-option value="2">已使用</a-select-option>
                <a-select-option value="3">已过期</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:add style="margin-left: 8px" icon="rollback" @click="handleGoBack">返回</a-button>
            </span>
          </a-col>
        </a-row>
        <a-row :gutter="48" style="margin-bottom: 30px">
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <a-button v-action:query type="primary" :loading="loading" @click="exportE()">导出已核销记录</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--  #####################查询条件###################  -->

    <!--  #####################操作###################  -->

    <!--  #####################操作###################  -->

    <!--  #####################表格###################  -->
    <s-table
      ref="table"
      :columns="columns"
      :data="loadData"
      bordered
    >

      <a-tag slot="status" slot-scope="text, record" :color="text | statusColorFilter">
        {{text | statusFilter}}
      </a-tag>
      <a-tag slot="masterType" slot-scope="text, record" :color="text == 2 ? '#87d068' : '#2db7f5'">
        {{text | masterTypeFilter}}
      </a-tag>

      <span slot="fileUrl" slot-scope="text, record">
        <img :src="text" alt="" height="100px">
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:edit @click="handleEdit(record)">编辑</a>
          <a-divider v-action:edit type="vertical" />
          <a v-action:edit @click="updateStatus(record.id)"> {{record.status == 1 ? '上架':'下架'}}</a>
          <a-divider v-action:edit type="vertical" />
          <a v-action:delete @click="handleDel(record)">删除</a>
        </template>
      </span>
    </s-table>
    <!--  #####################表格###################  -->

    <!--  #####################打开的页面###################  -->

    <!--  #####################打开的页面###################  -->
  </div>
</template>

<script>
  import moment from 'moment'
  import { STable } from '@/components'
  import { selectPage, exportExcel } from '@/api/shop/couponOrder'

  const statusMap = {
    1: {
      status: '',
      text: '未使用'
    },
    2: {
      status: '#2db7f5',
      text: '已使用'
    },
    3: {
      status: '#f50',
      text: '已过期'
    }
  }
  export default {
    name: 'menuList',
    components: {
      STable
    },
    props: {
      record: {
        type: [Object, String],
        default: ''
      }
    },
    data () {
      return {
        treeData: [],
        // 高级搜索 展开/关闭
        advanced: false,
        loading: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '代金券名称',
            dataIndex: 'stockName'
          },
          {
            title: '编号',
            dataIndex: 'couponCode'
          },
          {
            title: '领取时间',
            dataIndex: 'createdTime',
            sorter: true
          },
          {
            title: '领取人',
            dataIndex: 'nickName'
          },
          {
            title: '核销状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '核销时间',
            dataIndex: 'consumeTime'
          },
          {
            title: '核销商户',
            dataIndex: 'merchantName'
          },
          {
            title: '核销订单号',
            dataIndex: 'transactionId'
          },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   width: '160px',
          //   scopedSlots: { customRender: 'action' }
          // }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          this.queryParam.stockId = this.record.stockId
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        }
      }
    },
    created() {

    },
    filters: {
      statusFilter (status) {
        return statusMap[status].text
      },
      statusColorFilter (status) {
        return statusMap[status].status
      },
    },
    methods: {
      handleOk () {
        this.$refs.table.refresh()
      },
      handleGoBack () {
        this.$emit('onGoBack')
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetSearchForm () {
        this.queryParam = {
          date: moment(new Date())
        }
      },
      exportE() {
        this.loading = true
        exportExcel({stockId: this.record.stockId}).then(res => {
          this.loading = false
          const link = document.createElement('a')
          let blob = new Blob([res], {type: 'application/vnd.ms-excel'});
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob);
          let num = ''
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10)
          }
          link.setAttribute('download', this.record.stockId + '.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        })
      }
    }
  }
</script>
